#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: set_seed_flag.py
@time: 2017/12/27 22:02
"""
import sys

sys.path.append('..')

from common.mongo import MongDb
from logger import Logger

MONGO_DB_SOURCE = {
    'host': '172.16.215.2',
    'port': 40042,
    'db': 'company_data',
    'username': 'work',
    'password': 'haizhi'
}

# 省份信息
PROVINCE_LIST = [
    'gansu',
    'gsxt',
    'hunan',
    'beijing',
    'tianjin',
    'hebei',
    'shandong',
    'guangxi',
    'qinghai',
    'jiangsu',
    'ningxia',
    'sichuan',
    'liaoning',
    'guangdong',
    'jilin',
    'shanxi',
    'anhui',
    'shanghai',
    'xizang',
    'hainan',
    'shanxicu',
    'neimenggu',
    'yunnan',
    'hubei',
    'zhejiang',
    'heilongjiang',
    'chongqing',
    'xinjiang',
    'henan',
    'guizhou',
    'fujian',
    'jiangxi',
]

# 日志模块
log = Logger('delete_data.log').get_logger()

source_db = MongDb(MONGO_DB_SOURCE['host'], MONGO_DB_SOURCE['port'], MONGO_DB_SOURCE['db'],
                   MONGO_DB_SOURCE['username'],
                   MONGO_DB_SOURCE['password'], log=log)


def main():
    table_template = 'new_{}_company_list'

    special_chart = r'\#%*'

    count = 0
    delete_count = 0
    for province in PROVINCE_LIST:
        source_table = table_template.format(province)
        for item in source_db.traverse_batch_field(source_table, {}, ['_id']):
            _id = item.get('_id')

            for c in special_chart:
                if c in _id:
                    delete_count += 1
                    source_db.delete(source_table, _id)
                    log.info("删除企业: _id = {}".format(_id))
                    break

            count += 1

            if count % 1000 == 0:
                log.info("当前完成进度: province = {} count = {} delete_count = {}".format(
                    province, count, delete_count))

        log.info("数据处理完成: province = {}".format(province))

    log.info("总共处理数据量: count = {}".format(count))


if __name__ == '__main__':
    main()
